package QueueDemo;

public class MyQueue {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode prev;

        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode first;
    public ListNode rear;
    public int useSize;

    public void offer(int val){
        ListNode node = new ListNode(val);

        if(first == null){
            first = node;
            rear = node;
            return;
        }

        rear.next = node;
        node.prev = rear;
        rear = node;

    }

    public int poll(){
        if(first == null){
            return -1;
        }
        int oldVal = first.val;
        first = first.next;
        if(first == null){
            rear = null;
        }else{
            first.prev = null;
        }
        useSize--;
        return oldVal;
    }

    public int peek(){
        if(first == null){
            return -1;
        }
        return first.val;
    }

    public boolean isEmpty(){
        return useSize == 0;
    }


}
